package cn.com.xyc.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import cn.com.xyc.pojo.CostLive;
import cn.com.xyc.pojo.WordBook;
import cn.com.xyc.service.MyLifeService;

public class MyLifeAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private MyLifeService myLifeService;
	@Autowired
	private EhCacheCacheManager cacheManager;
	
	private String tipInfo;
	private String check;

	private List<WordBook> wordBookList;// 字典
	private String costInfoStr; // 保存新增加的消费信息
	private List<CostLive> costLiveList;// 消费信息列表
	private List<CostLive> costLiveListOfSelect2;// 消费信息列表（月统计信息）
	private Map<String, String> costCountInfos;// 当前年 月周日的消费统计信息
	private Map<String, String> publicMap;
	private InputStream inputStream;
	private String exportFileName;

	
	
	/************************************************** 开销管理模块  start***********************************/
	/**
	 * 跳转到今日开销页面
	 * 
	 * @return
	 */
	public String costInitPage() {
		try {
			wordBookList = myLifeService.selectWordBookListByType("COST_TYPE");// 获取 开销类别信息
			costCountInfos = myLifeService.selectCostCountInfos();// 获取当前年、当前月、当前周、当前日的开销统计信息
			CostLive costLive = new CostLive();
			costLive.setCostDate(new Date());
			costLiveList = myLifeService.selectCostLivesList(costLive);// 当天开销信息
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "costLiveInsert";
	}

	/**
	 * 保存今日开销
	 * 
	 * @return
	 */
	public String saveCostInfoToday() {
		try {
			boolean i = myLifeService.saveTodayCostInfo(costInfoStr,
					getCurrentUser());
			if (i) {
				check = "1";
			} else {
				check = "0";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "saveCostInfoTodaySuccess";
	}

	/**
	 * 开销查询
	 * 
	 * @return
	 */
	public String costInfoList() {
		try {
			if (publicMap == null) {
				publicMap = new HashMap<String, String>();
			}
			publicMap.put("selectCount", "0");
			costLiveList = myLifeService.selectCostLivesList(publicMap);
			if (check != null) {
				if (check.equals("1")) {
					tipInfo = "今日消费更新成功！";
				} else {
					tipInfo = "今日消费更新失败!";
				}
			}
		} catch (Exception e) {
		}
		return "costInfoList";
	}

	public String costExportInit() {
		try {
			if (publicMap == null) {
				publicMap = new HashMap<String, String>();
			}
			publicMap.put("selectCount", "0");// 普通查询统计
			publicMap.put("costDateOfCount", "0");// 月查询统计

			costLiveList = myLifeService.selectCostLivesList(publicMap);
			wordBookList = myLifeService.selectWordBookListByType("COST_TYPE");// 获取
																				// 开销类别信息
			costCountInfos = myLifeService.selectCostCountInfos();// 获取当前年、当前月、当前周、当前日的开销统计信息

			costLiveListOfSelect2 = myLifeService
					.selectCostLivesListOfSelect2(publicMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "costExportInitPage";
	}

	public String exportSelectInfos() {
		try {
			if (publicMap != null
					&& StringUtils.isNotBlank(publicMap.get("exportRowId"))) {
				HSSFWorkbook workbook = new HSSFWorkbook();
				myLifeService.exportSelectCostInfos(
						publicMap.get("exportRowId"), workbook);

				ByteArrayOutputStream output = new ByteArrayOutputStream();
				workbook.write(output);
				byte[] ba = output.toByteArray();
				inputStream = new ByteArrayInputStream(ba);
				output.flush();
				output.close();
				exportFileName = "消费信息表.xls";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "exportExcel";
	}

	public String exportInfosByExample() {
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			publicMap.put("selectCount", "0");// 普通查询统计
			publicMap.put("costDateOfCount", "0");// 月查询统计

			if (publicMap.get("opt").equals("1")) {
				costLiveList = myLifeService.selectCostLivesList(publicMap);
			} else {
				costLiveList = myLifeService
						.selectCostLivesListOfSelect2(publicMap);
			}
			myLifeService.exportCostInfosByExample(workbook, costLiveList);

			ByteArrayOutputStream output = new ByteArrayOutputStream();
			workbook.write(output);
			byte[] ba = output.toByteArray();
			inputStream = new ByteArrayInputStream(ba);
			output.flush();
			output.close();
			exportFileName = "消费信息表.xls";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "exportExcel";
	}

	/************************************************** 开销管理模块  end***********************************/
	
	
	
	// 属性GET SET
	public List<WordBook> getWordBookList() {
		return wordBookList;
	}

	public void setWordBookList(List<WordBook> wordBookList) {
		this.wordBookList = wordBookList;
	}

	public String getCostInfoStr() {
		return costInfoStr;
	}

	public void setCostInfoStr(String costInfoStr) {
		this.costInfoStr = costInfoStr;
	}

	public List<CostLive> getCostLiveList() {
		return costLiveList;
	}

	public void setCostLiveList(List<CostLive> costLiveList) {
		this.costLiveList = costLiveList;
	}

	public List<CostLive> getCostLiveListOfSelect2() {
		return costLiveListOfSelect2;
	}

	public void setCostLiveListOfSelect2(List<CostLive> costLiveListOfSelect2) {
		this.costLiveListOfSelect2 = costLiveListOfSelect2;
	}

	public Map<String, String> getCostCountInfos() {
		return costCountInfos;
	}

	public void setCostCountInfos(Map<String, String> costCountInfos) {
		this.costCountInfos = costCountInfos;
	}

	public Map<String, String> getPublicMap() {
		return publicMap;
	}

	public void setPublicMap(Map<String, String> publicMap) {
		this.publicMap = publicMap;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getExportFileName() {
		try {
			exportFileName = new String(exportFileName.getBytes("UTF-8"),
					"iso8859-1");
			exportFileName = java.net.URLEncoder.encode(exportFileName,
					"iso8859-1");
		} catch (UnsupportedEncodingException e) {
		}
		return exportFileName;
	}

	public void setExportFileName(String exportFileName) {
		this.exportFileName = exportFileName;
	}

	public String getTipInfo() {
		return tipInfo;
	}

	public void setTipInfo(String tipInfo) {
		this.tipInfo = tipInfo;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}
	
	

}
